import React from 'react'
import { validate } from '@formily/validator'
import { createForm, FormPath } from '@formily/core'
import { attach } from '@formily/core/src/__tests__/shared'

// window.parse2 = FormPath.parse

// validate(
//   '123',
//   (value, rules, context, format) => {
//     console.log('value: ', value)
//     console.log('rules: ', rules)
//     console.log('context: ', context)
//     console.log('format: ', format('124{{name}}'))
//     return '1'
//   },
//   {
//     context: {
//       name: 'james',
//     },
//   }
// )

const form = attach(createForm())

const field2 = attach(
  form.createField({
    name: 'bb',
    required: true,
    // value 为空的话，以下 validator 的三个都会校验通过
    value: '111',
    validator: [
      (value) => {
        if (value == '123') {
          return {
            type: 'success',
            message: 'success',
          }
        } else if (value == '321') {
          return {
            type: 'warning',
            message: 'warning',
          }
        } else if (value == '111') {
          return 'error'
        }
      },
      {
        triggerType: 'onBlur',
        format: 'url',
      },
      {
        triggerType: 'onFocus',
        format: 'date',
      },
    ],
  })
)

const field4 = attach(
  form.createField({
    name: 'ppp',
    required: true,
    basePath: 'bb',
  })
)

;(async () => {
  try {
    await field2.validate()
  } catch {}

  // console.log('field2: ', field2.selfErrors)
  // console.log('field4: ', field4.selfErrors)
})()

export default () => <div>index-test</div>
